package com.cloudkinto.service.stock.impl;

import com.cloudkinto.common.BeanConvert;
import com.cloudkinto.dao.CountryCodeDao;
import com.cloudkinto.dao.JpRegionDao;
import com.cloudkinto.entity.CountryCodeDo;
import com.cloudkinto.entity.JpRegionDo;
import com.cloudkinto.entity.ShippingAddressDo;
import com.cloudkinto.dao.ShippingAddressDao;
import com.cloudkinto.entity.StorageDo;
import com.cloudkinto.service.stock.ShippingAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import com.cloudkinto.service.stock.vo.ShippingAddressAddReq;
import com.cloudkinto.service.stock.vo.ShippingAddressDetailRes;
import com.cloudkinto.service.stock.vo.ShippingAddressUpdateReq;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  service实现类
 * </p>
 * @author zhujiale
 * @since 2022-09-20
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class ShippingAddressServiceImpl extends ServiceImpl<ShippingAddressDao, ShippingAddressDo> implements ShippingAddressService {
    @Autowired
    private ShippingAddressDao dao;
    @Autowired
    private CountryCodeDao countryCodeDao;
    @Autowired
    private JpRegionDao jpRegionDao;

    @Override
    public SingleResult addInit() {
        Map result = new HashMap();

        List<Map> countryList = new ArrayList<>();
        List<JpRegionDo> countryDoList = jpRegionDao.selectList(null);
        if (!CollectionUtils.isEmpty(countryDoList)) {
            countryDoList.forEach(countryDo -> {
                Map map = new HashMap();
                map.put("id", countryDo.getId());
                map.put("value", countryDo.getName());
                countryList.add(map);
            });
        }
        result.put("countryList", countryList);
        return SingleResult.success(result);
    }

    @Override
    public Object add(ShippingAddressAddReq req, Long userId, Long companyId) {
        ShippingAddressDo entityDo = BeanConvert.INSTANCE.shippingAddressDo(req);
        entityDo.setCompanyId(companyId);
        entityDo.setCreateBy(userId);
        entityDo.setCreateTime(new Date());
        dao.insert(entityDo);
        return entityDo;
    }

    @Override
    public Object update(ShippingAddressUpdateReq req, Long userId) {
        ShippingAddressDo entityDo = BeanConvert.INSTANCE.shippingAddressUpdateDo(req);
        entityDo.setUpdateBy(userId);
        entityDo.setUpdateTime(new Date());
        dao.updateById(entityDo);
        return entityDo;
    }

    @Override
    public int delete(Long id) {
        return dao.deleteById(id);
    }


    @Override
    public Object detail(Long id) {
        ShippingAddressDo entityDo =  dao.selectById(id);
        ShippingAddressDetailRes res = BeanConvert.INSTANCE.shippingAddressDetailRes(entityDo);
        JpRegionDo countryCodeDo = jpRegionDao.selectById(res.getAreaId());
        if (countryCodeDo != null) {
            res.setAreaCN(countryCodeDo.getName());
        }
        return res;
    }

    @Override
    public PageResult getListPage(Map<String, String> map) {
        QueryWrapper<ShippingAddressDo> wrapper = queryBuild(map);
        IPage<ShippingAddressDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<ShippingAddressDo> list = page.getRecords();
        List<ShippingAddressDetailRes> resList = new ArrayList<>();
        list.forEach(et ->{
            ShippingAddressDetailRes res = BeanConvert.INSTANCE.shippingAddressDetailRes(et);
            JpRegionDo jpRegionDo = jpRegionDao.selectById(res.getAreaId());
            if (jpRegionDo != null) {
                res.setAreaCN(jpRegionDo.getName());
            }
            resList.add(res);
        });
        PageResult result = new PageResult(resList, (int)page.getCurrent(), (int)page.getSize(), (int)page.getTotal(), (int)page.getPages());
        return result;
    }


    private IPage<ShippingAddressDo> pageInit(Map<String, String> map) {
        IPage<ShippingAddressDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<ShippingAddressDo> queryBuild(Map<String, String> map) {
        QueryWrapper<ShippingAddressDo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(map.get("companyId"))) {
            wrapper.eq("company_id", map.get("companyId"));
        }
        if (StringUtils.isNotBlank(map.get("keyword1"))) {
            wrapper.like("contact", map.get("keyword1")).or();
            wrapper.like("company_name", map.get("keyword1")).or();
            wrapper.like("address", map.get("keyword1"));
        }
        if (StringUtils.isNotBlank(map.get("keyword2"))) {
            wrapper.like("post_code", map.get("keyword2")).or();
            wrapper.like("tel_phone", map.get("keyword2"));
        }
        if (StringUtils.isNotBlank(map.get("areaId"))) {
            wrapper.eq("area_id", map.get("areaId"));
        }
        return wrapper;
    }


}
